<?php



    //$content需要在引用的时候被定义

    //$content="输出10个水果";

    $_code=date("Ymdhis");

    $url = 'https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions';

// 若没有配置环境变量，请用百炼API Key将下行替换为：$apiKey = "sk-xxx";
#$apiKey = getenv('DASHSCOPE_API_KEY');
    $apiKey =  "sk-6b9ebd5e8e5d406d8e0e69ec16ea3766";


// 设置请求头
    $headers = [
        'Authorization: Bearer '.$apiKey,
        'Content-Type: application/json'
    ];
// 设置请求体
    $para_data = [
        // 模型列表：https://help.aliyun.com/zh/model-studio/getting-started/models
        //"model" => "qwen-plus",
        //"model" => "qwen-max",
        "model" => "qwen-turbo",
        "messages" => [
            [
                "role" => "system",
                "content" => "You are a facebook editor.".$_code
            ],
            [
                "role" => "user",
                "content" => $content
            ]
        ]
    ];
// 初始化cURL会话
    $ch = curl_init();
// 设置cURL选项
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($para_data));
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//返回不输出结果
// 执行cURL会话
    $response = curl_exec($ch);
// 检查是否有错误发生
    if (curl_errno($ch)) {
        echo 'Curl error: ' . curl_error($ch);
    }
// 关闭cURL资源
    curl_close($ch);
// 输出响应结果


    if ($response === false) {
        echo 'Curl error: ' . curl_error($ch);
    }





    $_result = json_decode($response, true);
    $_result=$_result['choices'][0]['message']['content'];



    //echo output($msg="get result",200,$_result);



?>
